package com.ecric.http.webService;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.ecric.http.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

@EActivity(R.layout.activity_my_weather_ws)
public class MyWeatherWSActivity extends AppCompatActivity {

    @ViewById
    TextView txt_weatherCurrent;
    @ViewById(R.id.sp_province)
    Spinner sp_province;
    @ViewById(R.id.sp_city)
    Spinner sp_city;
    @ViewById
    ImageView img_today_ws1;
    @ViewById
    ImageView img_today_ws2;
    @ViewById
    TextView txt_today_weather;
    @ViewById
    ImageView img_tomorrow_ws1;
    @ViewById
    ImageView img_tomorrow_ws2;
    @ViewById
    TextView txt_tomorrow_weather;
    @ViewById
    ImageView img_yesterday_ws1;
    @ViewById
    ImageView img_yesterday__ws2;
    @ViewById
    TextView txt_yesterday__weather;
    private  List<String> provinceList =  new ArrayList<>();


    @AfterViews
    void initData(){

        //调用远程web service 获取省份列表
        List<String> provinces = WebServiceWeatherUtil.getProvinceList();
        if (provinces != null){
            provinceList.addAll(provinces);
        }
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item_txt_layout,provinceList);
        sp_province.setAdapter(adapter);
        sp_province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                List<String> cities = WebServiceWeatherUtil.getCityListByProvince(sp_province.getSelectedItem().toString());
                ArrayAdapter adapter = new ArrayAdapter(MyWeatherWSActivity.this, R.layout.item_txt_layout,cities);
                sp_city.setAdapter(adapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                showWeather(sp_city.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void showWeather(String city) {
        String weatherToday = null;
        String weatherYesterday = null;
        String weatherTomoorow = null;
        String weatherCurrent = null;
        int iconToday[] = new int[2];
        int iconTomorrow[] = new int[2];
        int iconYesterDay[] = new int[2];
        //获取远程web service返回的对象
        SoapObject detail = WebServiceWeatherUtil.getWeatherByCity(city);
        //获取天气实况
        weatherCurrent = detail.getProperty(4).toString();
        //解析今天的天气情况
        String date = detail.getProperty(7).toString();
        weatherToday = "今天："  + date.split(" ")[0];
        weatherToday = weatherToday + "\n天气：" + date.split(" ")[1];
        weatherToday = weatherToday + "\n气温：" + detail.getProperty(8).toString();
        weatherToday = weatherToday + "\n风力：" + detail.getProperty(9).toString() + "\n";
        iconToday[0] = parseIcon(detail.getProperty(10).toString());
        iconToday[1] = parseIcon(detail.getProperty(11).toString());
        //解析明天的天气情况
        date = detail.getProperty(12).toString();
        weatherTomoorow = "今天："  + date.split(" ")[0];
        weatherTomoorow = weatherTomoorow + "\n天气：" + date.split(" ")[1];
        weatherTomoorow = weatherTomoorow + "\n气温：" + detail.getProperty(13).toString();
        weatherTomoorow = weatherTomoorow + "\n风力：" + detail.getProperty(14).toString() + "\n";
        iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());
        iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());
        //解析后天的天气情况
        date = detail.getProperty(17).toString();
        weatherYesterday = "今天："  + date.split(" ")[0];
        weatherYesterday = weatherYesterday + "\n天气：" + date.split(" ")[1];
        weatherYesterday = weatherYesterday + "\n气温：" + detail.getProperty(18).toString();
        weatherYesterday = weatherYesterday + "\n风力：" + detail.getProperty(19).toString() + "\n";
        iconYesterDay[0] = parseIcon(detail.getProperty(20).toString());
        iconYesterDay[1] = parseIcon(detail.getProperty(21).toString());

        txt_weatherCurrent.setText(weatherCurrent);
        txt_today_weather.setText(weatherToday);
        img_today_ws1.setImageResource(iconToday[0]);
        img_today_ws2.setImageResource(iconToday[1]);

        txt_tomorrow_weather.setText(weatherTomoorow);
        img_tomorrow_ws1.setImageResource(iconTomorrow[0]);
        img_tomorrow_ws2.setImageResource(iconTomorrow[1]);

        txt_yesterday__weather.setText(weatherYesterday);
        img_yesterday_ws1.setImageResource(iconYesterDay[0]);
        img_yesterday__ws2.setImageResource(iconYesterDay[1]);

    }

    private int parseIcon(String s) {
        //省略了根据字符集解析天气图标的代码
        return R.mipmap.ic_launcher;
    }


}
